package com.gitee.wsl.compose.calendar

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.gitee.wsl.compose.chart.sample.SampleView
import com.gitee.wsl.compose.ui.calendar.CalendarAdapter
import com.gitee.wsl.compose.ui.calendar.Horizontal
import com.gitee.wsl.compose.ui.calendar.rememberCalendarState
import com.gitee.wsl.compose.ui.calendar.view.Day
import com.gitee.wsl.compose.ui.calendar.view.MonthHeaderFooter
import com.gitee.wsl.compose.ui.calendar.view.defaultDayAdapterUseCircle
import com.gitee.wsl.compose.ui.calendar.view.defaultWeekHeader
import com.gitee.wsl.compose.ui.calendar.view.useAdapter
import com.gitee.wsl.time.YearMonth
import com.gitee.wsl.time.calendar.Calendar
import com.gitee.wsl.time.minusMonths
import com.gitee.wsl.time.nextMonth
import com.gitee.wsl.time.plusMonths
import com.gitee.wsl.time.previousMonth
import kotlinx.coroutines.launch
import kotlin.time.ExperimentalTime

val calendarSampleView = object : SampleView {
    override val name: String = "calendar View"

    override val thumbnail = @Composable {
        Example1Page()
    }

    override val content: @Composable () -> Unit = @Composable {
        Example10Page()
    }
}


@OptIn(ExperimentalTime::class)
@Composable
fun Example1Page(adjacentMonths: Int = 500) {
    val currentMonth = remember { YearMonth.now() }
    val startMonth = remember { currentMonth.minusMonths(adjacentMonths) }
    val endMonth = remember { currentMonth.plusMonths(adjacentMonths) }
    //val selections = remember { mutableStateListOf<LocalDate>() }
    val adapter = remember { CalendarAdapter() }
    //val daysOfWeek = remember { daysOfWeek() }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White),
    ) {
        val state = rememberCalendarState(
            startMonth = startMonth,
            endMonth = endMonth,
            firstVisibleMonth = currentMonth,
            //firstDayOfWeek = daysOfWeek.first(),
        )
        val coroutineScope = rememberCoroutineScope()
        val visibleMonth = rememberFirstMostVisibleMonth(state, viewportPercent = 90f)
        SimpleCalendarTitle(
            modifier = Modifier.padding(vertical = 10.dp, horizontal = 8.dp),
            currentMonth = visibleMonth.yearMonth,
            goToPrevious = {
                coroutineScope.launch {
                    state.animateScrollToMonth(state.firstVisibleMonth.yearMonth.previousMonth)
                }
            },
            goToNext = {
                coroutineScope.launch {
                    state.animateScrollToMonth(state.firstVisibleMonth.yearMonth.nextMonth)
                }
            },
        )
        Calendar.Horizontal(
            modifier = Modifier.testTag("Calendar"),
            state = state,
//            dayContent = { day ->
//                Day(day, isSelected = selections.contains(day)) { clicked ->
//                    if (selections.contains(clicked)) {
//                        selections.remove(clicked)
//                    } else {
//                        selections.add(clicked)
//                    }
//                }
//            },
            dayContent = Day.useAdapter(adapter,Day.defaultDayAdapterUseCircle),
            monthHeader = MonthHeaderFooter.defaultWeekHeader
//                { week,local ->
//                MonthHeader(daysOfWeek = daysOfWeek)
//            },
        )
    }
}


